home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / chapter10 / dynamic.java < prev    next >
Text File  |  1995-12-31  |  673b  |  33 lines

  1. /* Dynamic Instantiation example */
  2. class Shape {}
  3. class rectangle extends Shape {
  4.     rectangle() {
  5.      System.out.println("rectangle instantiated");
  6.      }
  7.     }
  8. class circle extends Shape {
  9.     circle() {
  10.      System.out.println("circle instantiated");
  11.      }
  12.     }
  13. class triangle extends Shape {
  14.     triangle() {
  15.      System.out.println("triangle instantiated");
  16.      }
  17.     }
  18.  
  19.  
  20.  
  21. /* Create a rectangle object "on the fly" */
  22. class dynamic {
  23.  
  24.     static public void main(String args[]) throws
  25.                 ClassNotFoundException, 
  26.                 InstantiationException, 
  27.                 IllegalAccessException {
  28.     
  29.      String S = "rectangle";
  30.      Shape myShape = (Shape)Class.forName(S).newInstance();
  31.     }
  32.       }    
  33.